package com.lucenten.collect.gnss.handler.impl;

import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.util.SpringUtil;
import com.lucenten.collect.exec.ShellRun;
import com.lucenten.collect.gnss.entity.GnssFile;
import com.lucenten.collect.gnss.entity.SatelliteStation;
import com.lucenten.collect.gnss.handler.AbstractGnssHandler;
import com.lucenten.collect.gnss.handler.GnssHandler;
import com.lucenten.collect.gnss.handler.tools.StationInfoTools;
import com.lucenten.collect.gnss.service.ObsErvationService;
import com.lucenten.collect.gnss.service.SatelliteStationService;
import com.lucenten.collect.utils.FileUtils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 第三步：.站点站台入库
 */
public class StorageGnssHandler extends AbstractGnssHandler implements GnssHandler {
    public StorageGnssHandler(SatelliteStationService satelliteStationService, ObsErvationService obsErvationService) {
        super(satelliteStationService, obsErvationService);
    }

    @Override
    protected boolean doProcess(GnssFile gnssFile) {
        // 如果没有需要入库的，则直接忽略
        if (StringUtils.isEmpty(gnssFile.getStorageStations()) || StringUtils.isEmpty(gnssFile.getDirFiles())) {
            return true;
        }
        Path oPath = null;
        final String infoPath = super.getTempPath("stationInfo",gnssFile);
        for (String fileName : gnssFile.getDirFiles()) {
            if (!fileName.endsWith("o")) {
                continue;
            }
            oPath = Paths.get(super.getTempPath("o",gnssFile),fileName);
            if (Files.notExists(oPath)) {
                continue;
            }
            storage(oPath, gnssFile,infoPath);
        }
        return true;
    }

    /**
     * 解析存储
     *
     * @param file
     * @param gnssFile
     * @param infoPath
     */
    private void storage(Path file, GnssFile gnssFile, String infoPath) {
        String stationName = super.getSetationName(file.getFileName().normalize().toString().toLowerCase());
        String key = gnssFile.getStationId() + "_" + stationName;
        if (!gnssFile.getStations().containsKey(key)) {
            gnssFile.getStorageStations().put(key, SatelliteStation.getInstance(gnssFile.getStationId(),stationName));
        }
        // 如果不在变更之列，则忽律
        if (!gnssFile.getStorageStations().containsKey(key)) {
            return;
        }
        Path infoFile = Paths.get(infoPath,"stationinfo_"+gnssFile.getStationId()+stationName+".txt");
        FileUtils.createDirs(infoFile);
        boolean isSuccess = ShellRun.execOutPut("teqc +meta "+file.normalize().toString(), infoFile);
        gnssFile.getClearList().add(infoFile.normalize().toString());
        // 判断是否转换成功
        if (!isSuccess) {
            return;
        }
        SatelliteStation station = gnssFile.getStorageStations().get(key);
        StationInfoTools.readStationInfoTxt(infoFile,station);
//        SatelliteStationService satelliteStationService = SpringUtil.getBean("satelliteStationService");
        satelliteStationService.create(station);
        gnssFile.getStations().put(station.getStationId()+","+station.getStationName(),station);
        gnssFile.setReStationName(station.getReStationName());
        gnssFile.setStation(station);
    }
}
